﻿using System.Xml.Linq;
using Nvigorate.Relational.Mapping;
using Nvigorate.Relational.Mapping.Index;
using Nvigorate.Relational.Mapping.Interface;
using Nvigorate.TestObjects;

namespace Nvigorate.Test.Relational
{
    /// <summary>
    /// The name is awful and so is the code :D
    /// </summary>
    class MapFake
    {
        public static void PopulateMaps()
        {
            var index = new MapIndex();
            //index.AddBindingMap((IBindingMap)new BindingMap().ParseXElement(XElement.Parse(_personMap)));
            index.AddBindingMap(new PersonMap().GetMap());
            index.AddBindingMap(new EmployeeMap().GetMap());
            index.AddBindingMap(new DepartmentMap().GetMap());
            index.AddBindingMap(new JobMap().GetMap());
            index.AddBindingMap(new VehicleMap().GetMap());

            //index.AddBindingMap((IBindingMap)new BindingMap().ParseXElement(XElement.Parse(_vehicleMap)));
            index.AddBindingMap((IBindingMap)new BindingMap().ParseXElement(XElement.Parse(_addressMap)));
            //index.AddBindingMap((IBindingMap)new BindingMap().ParseXElement(XElement.Parse(_manufacturerMap)));
            index.AddBindingMap((IBindingMap)new BindingMap().ParseXElement(XElement.Parse(_entryMap)));
            index.AddBindingMap((IBindingMap)new BindingMap().ParseXElement(XElement.Parse(_authorMap)));
        }

        
        private static string _entryMap =
            @"
<map target=""Nvigorate.TestObjects.Entry, Nvigorate.TestObjects"" repositoryName=""simple"">
    <sources>
        <source name=""Entry""  >
            <assignment target=""Id"" source=""EntryId"" targetKey=""true"" sourceKey=""true"" persistable=""false"" />
            <assignment target=""Name"" source=""EntryName"" />
        </source>
    </sources>
    <relatesTo>
        <relationship type=""peer"" relative=""Nvigorate.TestObjects.Author, Nvigorate.TestObjects"" relativeProperty=""Contributors"">
            <relationTable name=""contributor"">
                <parentAssignment target=""Id"" source=""EntryId"" />
                <childAssignment target=""Id"" source=""AuthorId"" />
            </relationTable>
        </relationship>
    </relatesTo>
</map>
";

        private static string _authorMap =
    @"
<map target=""Nvigorate.TestObjects.Author, Nvigorate.TestObjects"" repositoryName=""simple"">
    <sources>
        <source name=""Author""  >
            <assignment target=""Id"" source=""AuthorId"" targetKey=""true"" sourceKey=""true"" persistable=""false"" />
            <assignment target=""Name"" source=""AuthorName"" />
        </source>
    </sources>
    <relatesTo>
        <relationship type=""peer"" relative=""Nvigorate.TestObjects.Entry, Nvigorate.TestObjects"" relativeProperty=""Contributions"">
            <relationTable name=""contributor"">
                <parentAssignment target=""Id"" source=""AuthorId"" />
                <childAssignment target=""Id"" source=""EntryId"" />                
            </relationTable>
        </relationship>
    </relatesTo>
</map>
";

        private static string _personMap =
            @"
<map target=""Nvigorate.TestObjects.Person, Nvigorate.TestObjects"" repositoryName=""simple"">
    <sources>
        <source name=""Person"" >
            <assignment target=""SocialSecurity"" source=""SocialSecurity"" targetKey=""true"" sourceKey=""true"" />
            <assignment target=""FirstName"" source=""FirstName"" />
            <assignment target=""LastName"" source=""LastName"" />
            <assignment target=""DateOfBirth"" source=""DateOfBirth"" />
        </source>
    </sources>
    <relatesTo>
        <relationship type=""parent"" relative=""Nvigorate.TestObjects.Vehicle, Nvigorate.TestObjects"">
            <constraint subjectField=""SocialSecurity"" relativeField=""OwnerSocial"" />
        </relationship>
    </relatesTo>
</map>
";

        private static string _vehicleMap =
            @"
<map target=""Nvigorate.TestObjects.Vehicle, Nvigorate.TestObjects"" repositoryName=""simple"">
    <sources>
        <source name=""Vehicle"" >
            <assignment target=""VIN"" source=""vehicleId"" targetKey=""true"" sourceKey=""true"" />
            <assignment target=""OwnerSocial"" source=""ownerSSN"" />
            <assignment target=""Model"" source=""model"" />
            <assignment target=""Year"" source=""year"" />
            <assignment target=""ManufacturerId"" source=""ManufacturerId"" />
        </source>
    </sources>
</map>
";

        private static string _manufacturerMap =
            @"
<map target=""Nvigorate.TestObjects.Manufacturer, Nvigorate.TestObjects"" repositoryName=""simple"">
    <sources>
        <source name=""Manufacturer"" >
            <assignment target=""Id"" source=""ManufacturerId"" targetKey=""true"" sourceKey=""true"" />
            <assignment target=""Name"" source=""ManufacturerName"" />
        </source>
    </sources>
    <relatesTo>
        <relationship type=""parent"" relative=""Nvigorate.TestObjects.Vehicle, Nvigorate.TestObjects"">
            <constraint subjectField=""Id"" relativeField=""ManufacturerId"" />
        </relationship>
    </relatesTo>
</map>
";

        private static string _addressMap =
                        @"
<map target=""Nvigorate.TestObjects.Address, Nvigorate.TestObjects"" repositoryName=""simple"">
    <sources>
        <source name=""Address""  >
            <assignment target=""AddressId"" source=""AddressId"" targetKey=""true"" sourceKey=""true"" persistable=""false"" />
            <assignment target=""Additional1"" source=""Additional1"" />
            <assignment target=""Additional2"" source=""Additional2"" />
            <assignment target=""StreetNumber"" source=""StreetNumber"" />
            <assignment target=""StreetName"" source=""StreetName"" />
            <assignment target=""ZipCode"" source=""ZipCode"" />
        </source>
        <source name=""ZipCode"" >
            <assignment target=""ZipCode"" source=""ZipCode"" sourceKey=""true"" />
            <assignment target=""City"" source=""City"" />
            <assignment target=""State"" source=""State"" />
            <relates to=""Address"">
                <criteria>
                    <clause>
                        <equal comparison=""Equal"" >
                            <field>
                                <column name=""ZipCode"" owner=""Address"" />
                            </field>
                            <values>
                                <column name=""ZipCode"" owner=""ZipCode"" />
                            </values>
                        </equal>
                    </clause>
                </criteria>
            </relates>
        </source>
    </sources>
</map>
";
    }
}
